#include "dataauth.h"
#include <QDate>

DataAuth *DataAuth::s_pInstance = NULL;
DataAuth *DataAuth::getInstance()
{
    if(!s_pInstance)
    {
        s_pInstance = new DataAuth;
    }
    return s_pInstance;
}

// 对外接口
bool DataAuth::authInfoIntegriy(QString &strErr,int feature,MonthAndInnerCardInfo info)
{
    bool authResult = false;
//    selectCard(strErr,authResult,inOrMon,feature,info);
    MonSelectFeature(strErr,authResult,feature,info);
    return authResult;
}
void DataAuth::MonSelectFeature(QString &strErr,bool &result,int feature,MonthAndInnerCardInfo info)
{
    switch (feature) {
    case FEATURE::ADD :
        monthAddAuth(strErr,result,info);
        break;
    case FEATURE::DELETE :
        monthDelAuth(strErr,result,info);
        break;
    case FEATURE::MODIFY :
        monthModifyAuth(strErr,result,info);
        break;
    case FEATURE::SELECT :
        monthSelectAuth(strErr,result,info);
    default:
        break;
    }
}


// 信息完整性检测
void DataAuth::monthAddAuth(QString &strErr,bool &result,MonthAndInnerCardInfo info)
{
     if(info.veh_plate.size() != 7 )
     {
         strErr = "车牌位数不正确";
         result = false;
         return;
     }
     if(info.veh_ownner.isEmpty())
     {
         strErr = "车主姓名不能为空";
         result = false;
         return;
     }
     if(QDate::fromString(info.veh_valid_start_time,"yyyy/MM/dd")
             < QDate::fromString(info.veh_valid_end_time,"yyyy/MM/dd"))
     {
         strErr = "授权开始时间不能晚育授权结束时间";
         result = false;
         return;
     }
     result = true;
}
void DataAuth::monthDelAuth(QString &strErr,bool &result,MonthAndInnerCardInfo info)
{
     if(info.veh_plate.isEmpty() && info.veh_ownner.isEmpty())
     {
         strErr = "删除操作车牌或车主能同时为空";
         result = false;
         return;
     }
     if(!info.veh_plate.isEmpty() &&  info.veh_plate.size() != 7 )
     {
         strErr = "车牌位数不正确";
         result = false;
         return;
     }
     result = true;
}
void DataAuth::monthModifyAuth(QString &strErr,bool &result,MonthAndInnerCardInfo info)
{
    if(QDate::fromString(info.veh_valid_start_time,"yyyy/MM/dd")
            > QDate::fromString(info.veh_valid_end_time,"yyyy/MM/dd"))
    {
        strErr = "授权开始时间不能大于授权结束时间";
        result = false;
        return;
    }
    result = true;
}
void DataAuth::monthSelectAuth(QString &strErr,bool &result,MonthAndInnerCardInfo info)
{
    if(info.veh_plate.isEmpty() && info.veh_ownner.isEmpty())
    {
        strErr = "查找操作车牌或车主能同时为空";
        result = false;
        return;
    }
    result = true;
}
